package com.spring.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Tang
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("asdadasdasdadas");
        String url=request.getRequestURI();
        if(!(url.contains("login")||url.contains("Login"))){
            if(request.getSession().getAttribute("CURRENT_USER")!=null){
                return true;
            }else{
                request.setAttribute("message","您还没有登录，请先登录");
                request.getRequestDispatcher("/user/login").forward(request,response);
            }
        }else{
            return true;
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("asdddddddddd");
    }
}
